package br.com.hstar.client.usuario;

import br.com.hstar.client.ApplicationMenu;
import br.com.hstar.client.ServiceFactory;
import br.com.hstar.client.TelaPrincipal;
import br.com.hstar.client.util.ClientSession;
import br.com.hstar.client.util.FormPanel;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;

public class LoginPanel extends FormPanel {
	private TextBox login;
	private PasswordTextBox senha;
	private Button loginButt;
	
	public LoginPanel(String _title) {
		super(_title);
		
		login 	  = addTextBox("Login:", 10, 20, "");
		senha 	  = addPassTextBox("Senha:", "");
		loginButt = addButton("OK");
		
		loginButt.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				 ServiceFactory.createUsuarioService().login(
						 login.getText(), senha.getText(), new LoginAsyncCallback());
			}
		});
	}

	/**
	 * Login callback
	 */
	private class LoginAsyncCallback implements AsyncCallback {
		public void onFailure(Throwable caught) {

		}

		public void onSuccess(Object result) {
			if(result == null) {
				Window.alert("Login ou senha inválidos !");
				login.setText("");
				senha.setText("");
			} else {
				UsuarioUI usu = (UsuarioUI)result;
				ClientSession.getInstance().setProperty("usuario", usu);

				//carrega o menu e a tela dos quartos.
				RootPanel.get("boxContent").clear();
				RootPanel.get("boxContent").add(TelaPrincipal.getInstance());
				
				//add menu bar
				RootPanel.get("navigation").clear();
				RootPanel.get("navigation").add(ApplicationMenu.getMenuBar());
				
				//adiciona o painel de menu da direita
				//RootPanel.get("subcontentBox").clear();
				//RootPanel.get("subcontentBox").add(QuartosMenu.getInstance());
			}
		}
	}
	
}
